/*-----------------------------------------------------------------------------------
Name: Sangyoon Park
Date: January 25 2023
This do file does : 
	Create tables for displacement effects
-----------------------------------------------------------------------------------*/
clear all
set matsize 10000
set maxvar 10000
set more off
capture log close


#delim ;


/*-----------------------------------------------------------------------------------
			 Set up
-----------------------------------------------------------------------------------*/
*** Setting up files;
	glo datafile "qu_analysis"; 	// type in the name of the datafile that you want to create;

*** Set up program;
 ** Number Format;
	capture program drop autoformat;
	program define autoformat;

	if int(`1')==`1' global format "%12.0f";
	else if abs(`1')>10 global format "%9.1f";
	else if abs(`1')>1 global format "%9.2f";
	else if abs(`1')>.1 global format "%9.3f";
	else if abs(`1')>.01 global format "%9.3f";
	else if abs(`1')>.001 global format "%9.3f";
	else if abs(`1')>.0001 global format "%9.4f";
	else if abs(`1')<.00001 global format "%9.5f";

	end;
	
 ** Tex Table format;
  	capture program drop head_foot;
	program define head_foot;

	syntax, [caption(str)] [label(str)] [notes(str)] [size(str)] [columns(str)] [sideways] [long];

	local estimates=r(names);
	if "`columns'"=="" local columns:word count `estimates';
	if "`sideways'"=="sideways" local table sidewaystable;
	if "`long'"=="long" local table longtable;
	else local table table;
	local col = `columns'+1;

	global header \begin{`table'}[H] \footnotesize \begin{center};
		if "`size'"!="" global header $header \begin{`size'};
		if "`caption'"!="" global header $header \caption{`caption'\label{table:`label'}} ;
		global header $header  ///;
		\begin{adjustbox}{max width=\textwidth} ///;
		\begin{threeparttable} ///;
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} ;
		global header $header ///;
		\begin{tabular}{l*{`columns'}{c}}\toprule[1.5pt];

	global header_mid \tabularnewline;

	global panel_title \multicolumn{`col'}{l}{Panel}\tabularnewline \midrule;

	global footer_mid \midrule ;
	global footer \bottomrule[1.2pt] ///
		\end{tabular} ;
		if "`notes'"!="" {;
			global footer $footer \begin{tablenotes}[flushleft] ///;
			`notes' ///;
			\end{tablenotes} ;
			};
		global footer $footer \end{threeparttable}\end{adjustbox};
		if "`size'"!="" global footer $footer \end{`size'};
		global footer $footer \end{center} \end{`table'} 	;
	end;

*** table setting;
	local stat_balance cells("mean(fmt(2)) b(fmt(3) star)" "sd(par fmt(2)) t(par fmt(2))") ///;
			label noobs collabels(none) nonote ;
	local stat_table cells("mean(fmt(2))" "sd(par fmt(2))") label collabels(none) nonote ;

	local reg_table_format cells(b(fmt(3) star) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///;
		stats(N r2 ymean, fmt(0 3 2) labels("N" "R-Squared" "Mean of Dep. Var."))  ///;
		label mlabels(none) collabels(none) nonotes;

	local reg_table_format2 cells(b(fmt(3) star) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///;
		stats(ymean, fmt(3) labels("Mean of Dep. Var."))  ///;
		label mlabels(none) collabels(none) nonotes;

	
*** Read data;
	
	use "${data_path}/FARMER_LONG_SETUP_V2", replace;
	
*** Drop control communes;

	drop if control_commune == 1;	
	
*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);

*** Cluster for standard errors;

	egen group_id = group(strata st_tgroup);

*** Generate treatment levels and interaction terms;

	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig;
	gen control = (st_tgroup == 1 | st_tgroup == 5);
	
	gen post = (round == 1 | round == 2);
	gen postXdist = post*dist_nearest_treated;
	gen postXfarmer_in2 = post*farmer_in2;
	gen postXtrader_in2 = post*trader_in2;
	
*** Drop large farms (farm size larger than five hectares);

	drop if ha_whitedragon >= 5;
	
*** Define local variables;
	
	local dummy_coef dum_tgroup2 dum_tgroup3 dum_tgroup4 dum_tgroup5 dum_tgroup6 dum_tgroup7 dum_tgroup8;
	local treat_coef tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;

	local baseline "age female edu_second exp_dragon ha_whitedragon tree_whitedragon anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base cost_fert_base cost_pest_base cost_facequip_base cost_labor_base cost_utility_base cost_total_base attrit_fu1 attrit_fu2";
	local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;
	local balance_displace age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base;	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var tr_farm "Farmer Training";
	lab var tr_int "Intermediary Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility (C.E.)";
	lab var tr_fXcert "C.E. $\times$ Farmer Training";
	lab var tr_iXcert "C.E. $\times$ Intermediary Training";
	lab var tr_jXcert "C.E $\times$ Joint Training";
	lab var std_knowledge "Farmer GAP Knowledge";
	lab var std_perception "Farmer Food Safety Awareness";
	lab var int_std_knowledge "Intermediary GAP Knowledge";
	lab var dist_nearest_treated "Distance to nearest jointly trained farmer (km)";
	lab var farmer_in2 "Number of farmers within 2km distance";
	lab var trader_in3 "Number of exporters within 3km distance";
	lab var trader_in2 "Number of exporters within 2km distance";
	lab var dist_jointtrader "Distance to nearest jointly trained exporter (km)";
	lab var postXdist "Post Training $\times$ Distance to nearest jointly trained farmer (km)";

tempfile displace;
save `displace';	
	
/*-----------------------------------------------------------------------------------
			 OUTCOME = REVENUE AND PROFIT; EXPLORE DISPLACEMENT EFFECTS (Online Appendix Figure A-2)
-----------------------------------------------------------------------------------*/

** Setting;
	loc titles "& Price & Volume & Revenue & Profit  \\ \cmidrule{2-5}";
	loc numbers "& (1) & (2) & (3) & (4)  \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(fixedeffect ymean_control r2 N, fmt(0 2 2 0) ///
		labels("Farmer Fixed Effect" "Control mean (in levels)" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Farm-gate Sales", pattern(1 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;
		
	local notes_displace_sales \item Notes: This table reports displacement effects on farm sales of farmers in no training groups. Data is based on farmer reports from the baseline survey and two follow-up surveys. Outcomes on price and volume are log transformed. Post Training refers to the two follow-up survey periods. All specifications include farmer density and exporter density interacted with post training period as well as farmer and survey round fixed effects. Standard errors are clustered by farmer group and reported in parentheses. * denotes statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		
		
replace revenue = revenue/1000000 if round == 0;
replace cost_costtotal = cost_costtotal/1000000 if round == 0;
replace profit = revenue - cost_costtotal if round == 0;
replace log_price = log(price) if round == 0;
replace price = price/1000 if round == 0;		
replace log_totalvolume = totalvolume/1000 if round == 0;
replace totalvolume = totalvolume/1000 if round == 0;
replace imp_revenue = imp_revenue/1000000 if round == 0;
replace imp_profit = imp_revenue  - cost_costtotal if round == 0;
		
		
set seed 1357;
gen randnum = runiform();
drop if price == .;
drop if totalvolume == .;
drop if cost_costtotal == .;
drop if revenue == .;
drop if imp_revenue == .;
	

	local outcome "price totalvolume";

		foreach y of local outcome {;
		preserve;
		keep if control == 1;
		sort log_`y' randnum;
		drop if _n <= 6 | _n > _N-6;

	
		eststo: qui areg log_`y' postXdist postXfarmer_in2 postXtrader_in2 certelig i.round if control == 1, absorb(farmerid) robust cluster(group_id);
		estadd ysumm;
		qui sum `y' if dum_tgroup1 == 1 & post == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd local fixedeffect "Yes", replace;
		restore;
		};
	
	
	local outcome "revenue imp_revenue profit imp_profit ";

		foreach y of local outcome {;
		preserve;
		keep if control == 1;
		sort `y' randnum;
		drop if _n <= 6 | _n > _N-6;

	
		eststo: qui areg `y' postXdist postXfarmer_in2 postXtrader_in2 certelig i.round if control == 1, absorb(farmerid) robust cluster(group_id);
		estadd ysumm;
		qui sum `y' if dum_tgroup1 == 1 & post == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd local fixedeffect "Yes", replace;
		restore;
		};		

** Table;
	loc colnum 4;
	local caption Displacement Effects on Farm Sales of Farmers in No Training Groups;
	local label displace_farmsales_result_pool;
	local notes `notes_displace_sales';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_did_displace_farmsales_roundpool.tex", replace ///
	keep(postXdist) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");
est clear;
clear;

/*-----------------------------------------------------------------------------------
			 		OUTCOME = GAP Audit, Pesiticide, Attribute Combined (Online Appendix Figure A-3)
-----------------------------------------------------------------------------------*/	

use `displace';

** Setting;
	loc titles0 " & GAP & Pesticide & Observable \\ ";
	loc titles1 " & Compliance & Mean & Attribute \\ ";
	loc titles2 " & Score & Residue & Index \\ \cmidrule(lr){2-4} ";	
	loc numbers "& (1) & (2) & (3) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(ymean_control ysd_control r2 N, fmt(2 2 2 0) ///
		labels("Control mean (Pass/Total)" "Control standard deviation" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles0'" "`titles1'" "`titles2'" "`numbers'") ;


	local notes_combineoutcomes \item Notes: 
	Column 1 is standardized audit score based on the control group's mean and standard deviation. ///
	Column 2 scales residue levels by the pesticide's Maximum Residue Limit (MRL) according to EU. ///
	Column 3 is the average of z-scores of six observable product attributes. ///
	All specifications include baseline farmer and exporter characteristics, farmer density and exporter density within two kilometers as well as strata and survey round fixed effects. Standard errors are clustered by farmer group and reported in parentheses. * denotes statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		
		
		
** Estimation;
	local outcome "score";

		foreach y of local outcome {;
	
		eststo: qui areg std_`y' dist_jointtrader farmer_in2 trader_in2 certelig `balance' `intbalance' i.round if control == 1, absorb(strata) robust cluster(group_id);
		estadd ysumm;
		qui sum audit_`y' if dum_tgroup1 == 1 & post == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		qui estadd local fixedeffect "Yes", replace;
		};
		
	local outcome "mean_std_pest_eu";

		foreach y of local outcome {;
	
		eststo: qui areg `y' dist_jointtrader farmer_in2 trader_in2 certelig `balance' `intbalance' i.round if control == 1 & round == 2, absorb(strata) robust cluster(group_id);
		estadd ysumm;
		qui sum `y' if dum_tgroup1 == 1 & post == 1 & mean_std_pest_eu != .;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		qui estadd local fixedeffect "Yes", replace;
		};		

	local outcome "meanquality";

		foreach y of local outcome {;
	
		eststo: qui areg std_`y' dist_jointtrader farmer_in2 trader_in2 certelig `balance' `intbalance' i.round if control == 1, absorb(strata) robust cluster(group_id);
		estadd ysumm;
		qui sum avg`y' if dum_tgroup1 == 1 & post == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		qui estadd local fixedeffect "Yes", replace;
		};		
		
** Table;
	loc colnum 3;
	local caption Spillover Effects on Technology Adoption and Quality Upgrading;
	local label audit_result_pool;
	local notes `notes_combineoutcomes';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_displace_gapaudit_roundpool.tex", replace ///
	keep(dist_jointtrader farmer_in2 trader_in2) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;	
clear;
